From 4ad1b33e095924bd4ccf79295999dd54edaaac37 Mon Sep 17 00:00:00 2001
From: Luca Boccassi <luca.boccassi@gmail.com>
Date: Thu, 5 Mar 2020 22:51:22 +0000
Subject: [PATCH] updates for php7.4 and php8.0 (#212)

From upstream commit: 4ad1b33e095924bd4ccf79295999dd54edaaac37

* travisci: enabled php7.4 and php8.0

* updates for php7.4 and php8.0

- travisci enabled php7.4 and php8.0
- removed now unused references to TSRMLS_*
  These flags were mostly already removed from the
  php7 codebase but some instances were still present.
  With php8 these produce compile errors.
- fix tests for php8 and php7.4
  New TypeErrors now get handled correctly in the test cases.
- fix memory corruption in zmq.c
  The conflicting line causes memory leaks on other php
  version and causes a segfault on php8 and php7.4
  The error was provocable with test case
  021-callbackwarning.phpt. After removing of the line
  valgrind showed no memory leak, so this line was probably
  redundant. Also if you compare with zmqsocket constructor
  this line is also not present.

Signed-off-by: Luca Boccassi <luca.boccassi@gmail.com>
Signed-off-by: Adam Duskett <aduskett@gmail.com>
---
 php_zmq_private.h                  |  4 ++--
 tests/016-callbackinvalidargs.phpt |  4 ++++
 tests/022-highwatermark.phpt       |  6 +++---
 tests/bug_gh_43.phpt               | 25 +++++++++++++++++--------
 zmq.c                              |  1 -
 zmq_device.c                       | 14 +++++++-------
 zmq_sockopt.c                      |  2 +-
 7 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/php_zmq_private.h b/php_zmq_private.h
index 49630e9..2e5cd3b 100644
--- a/php_zmq_private.h
+++ b/php_zmq_private.h
@@ -156,9 +156,9 @@ typedef struct _php_zmq_device_object  {
 
 #define PHP_ZMQ_ERROR_HANDLING_INIT() zend_error_handling error_handling;
 
-#define PHP_ZMQ_ERROR_HANDLING_THROW() zend_replace_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry, &error_handling TSRMLS_CC);
+#define PHP_ZMQ_ERROR_HANDLING_THROW() zend_replace_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry, &error_handling);
 
-#define PHP_ZMQ_ERROR_HANDLING_RESTORE() zend_restore_error_handling(&error_handling TSRMLS_CC);
+#define PHP_ZMQ_ERROR_HANDLING_RESTORE() zend_restore_error_handling(&error_handling);
 
 /* Compatibility macros between zeromq 2.x and 3.x */
 #ifndef ZMQ_DONTWAIT
diff --git a/tests/016-callbackinvalidargs.phpt b/tests/016-callbackinvalidargs.phpt
index a940e41..6bd0e75 100644
--- a/tests/016-callbackinvalidargs.phpt
+++ b/tests/016-callbackinvalidargs.phpt
@@ -10,6 +10,8 @@ try {
 	echo "Fail\n";
 } catch (ZMQSocketException $e) {
 	echo "OK\n";
+} catch (TypeError $e) {
+ 	echo "OK\n"; // on PHP8
 }
 
 try {
@@ -18,6 +20,8 @@ try {
 	echo "Fail\n";
 } catch (ZMQSocketException $e) {
 	echo "OK\n";
+} catch (TypeError $e) {
+ 	echo "OK\n"; // on PHP8
 }
 
 --EXPECT--
diff --git a/tests/022-highwatermark.phpt b/tests/022-highwatermark.phpt
index 84be509..c1ff703 100644
--- a/tests/022-highwatermark.phpt
+++ b/tests/022-highwatermark.phpt
@@ -1,11 +1,11 @@
 --TEST--
 Test that high-watermark works
 --SKIPIF--
-<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
-
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
 if (!defined('ZMQ::SOCKOPT_LINGER'))
     die ("Skip Not compiled against new enough version");
-
+?>
 --FILE--
 <?php
 
diff --git a/tests/bug_gh_43.phpt b/tests/bug_gh_43.phpt
index bdc274a..923d074 100644
--- a/tests/bug_gh_43.phpt
+++ b/tests/bug_gh_43.phpt
@@ -7,16 +7,25 @@ Test for Github issue #43
 --FILE--
 <?php
 
+error_reporting(0);
+
 $context = new ZMQContext (1, false);
 
 $sock1 = new ZMQSocket ($context, ZMQ::SOCKET_PUB);
 $sock2 = new ZMQSocket ($context, ZMQ::SOCKET_SUB);
 
-$device = new ZMQDevice ($sock1, $sock1, $sock1, $sock1);
-
-echo "OK";
-?>
-
---EXPECTF--
-Warning: ZMQDevice::__construct() expects at most 3 parameters, 4 given in %s/bug_gh_43.php on line %d
-OK
\ No newline at end of file
+try {
+    $device = new ZMQDevice ($sock1, $sock1, $sock1, $sock1);
+    // on PHP7 and lower
+    $lastError = error_get_last();
+    if(strpos($lastError['message'], 'ZMQDevice::__construct() expects at most 3 parameters, 4 given') !== false)
+     	echo "OK\n";
+    else{
+        echo "FAIL\n";
+        print_r($lastError);
+    }
+}catch(TypeError $e){
+ 	echo "OK\n"; // on PHP8
+}
+--EXPECT--
+OK
diff --git a/zmq.c b/zmq.c
index 942e69b..66196ea 100644
--- a/zmq.c
+++ b/zmq.c
@@ -687,7 +687,6 @@ PHP_METHOD(zmqcontext, getsocket)
 			if (!php_zmq_connect_callback(return_value, &fci, &fci_cache, persistent_id)) {
 				php_zmq_socket_destroy(socket);
 				interns->socket = NULL;
-				zval_dtor(return_value);
 				return;
 			}
 		}
diff --git a/zmq_device.c b/zmq_device.c
index c7415c1..534f966 100644
--- a/zmq_device.c
+++ b/zmq_device.c
@@ -41,7 +41,7 @@
 ZEND_EXTERN_MODULE_GLOBALS(php_zmq)
 
 static
-zend_bool s_invoke_device_cb (php_zmq_device_cb_t *cb, uint64_t current_ts TSRMLS_DC)
+zend_bool s_invoke_device_cb (php_zmq_device_cb_t *cb, uint64_t current_ts)
 {
 	zend_bool retval = 0;
 	zval params[1];
@@ -59,7 +59,7 @@ zend_bool s_invoke_device_cb (php_zmq_device_cb_t *cb, uint64_t current_ts TSRML
 	if (zend_call_function(&(cb->fci), &(cb->fci_cache)) == FAILURE) {
 		if (!EG(exception)) {
 			char *func_name = php_zmq_printable_func(&cb->fci, &cb->fci_cache);
-			zend_throw_exception_ex(php_zmq_device_exception_sc_entry_get (), 0 TSRMLS_CC, "Failed to invoke device callback %s()", func_name);
+			zend_throw_exception_ex(php_zmq_device_exception_sc_entry_get (), 0, "Failed to invoke device callback %s()", func_name);
 			zval_ptr_dtor(&params[0]);
 			efree(func_name);
 		}
@@ -94,7 +94,7 @@ int s_capture_message (void *socket, zmq_msg_t *msg, int more)
 }
 
 static
-int s_calculate_timeout (php_zmq_device_object *intern TSRMLS_DC)
+int s_calculate_timeout (php_zmq_device_object *intern)
 {
 	int timeout = -1;
 	uint64_t current = php_zmq_clock (ZMQ_G (clock_ctx));
@@ -131,7 +131,7 @@ int s_calculate_timeout (php_zmq_device_object *intern TSRMLS_DC)
 }
 
 
-zend_bool php_zmq_device (php_zmq_device_object *intern TSRMLS_DC)
+zend_bool php_zmq_device (php_zmq_device_object *intern)
 {
 	int errno_;
 	uint64_t last_message_received;
@@ -186,7 +186,7 @@ zend_bool php_zmq_device (php_zmq_device_object *intern TSRMLS_DC)
 		uint64_t current_ts = 0;
 
 		/* Calculate poll_timeout based on idle / timer cb */
-		int timeout = s_calculate_timeout (intern TSRMLS_CC);
+		int timeout = s_calculate_timeout (intern);
 
 		rc = zmq_poll(&items [0], 2, timeout);
 		if (rc < 0) {
@@ -205,7 +205,7 @@ zend_bool php_zmq_device (php_zmq_device_object *intern TSRMLS_DC)
 		if (intern->timer_cb.initialized && intern->timer_cb.timeout > 0) {
 			/* Is it timer to call the timer ? */
 			if (intern->timer_cb.scheduled_at <= current_ts) {
-				if (!s_invoke_device_cb (&intern->timer_cb, current_ts TSRMLS_CC)) {
+				if (!s_invoke_device_cb (&intern->timer_cb, current_ts)) {
 					zmq_msg_close (&msg);
 					return 1;
 				}
@@ -217,7 +217,7 @@ zend_bool php_zmq_device (php_zmq_device_object *intern TSRMLS_DC)
 			/* Is it timer to call the idle callback ? */
 			if ((current_ts - last_message_received) >= intern->idle_cb.timeout &&
 				intern->idle_cb.scheduled_at <= current_ts) {
-				if (!s_invoke_device_cb (&intern->idle_cb, current_ts TSRMLS_CC)) {
+				if (!s_invoke_device_cb (&intern->idle_cb, current_ts)) {
 					zmq_msg_close (&msg);
 					return 1;
 				}
diff --git a/zmq_sockopt.c b/zmq_sockopt.c
index 1357032..14b59f0 100644
--- a/zmq_sockopt.c
+++ b/zmq_sockopt.c
@@ -2036,7 +2036,7 @@ PHP_METHOD(zmqsocket, setsockopt)
     long key;
     zval *zv;
 
-    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz/", &key, &zv) == FAILURE) {
+    if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz/", &key, &zv) == FAILURE) {
         return;
     }
 
-- 
2.31.1

